home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / µSim 1.0.5 / source / Preferences.c < prev    next >
Encoding:
Text File  |  1995-12-06  |  7.8 KB  |  276 lines  |  [TEXT/CWIE]

  1. /*
  2. Copyright © 1993,1994,1995 Fabrizio Oddone
  3. ••• ••• ••• ••• ••• ••• ••• ••• ••• •••
  4. This source code is distributed as freeware:
  5. you may copy, exchange, modify this code.
  6. You may include this code in any kind of application: freeware,
  7. shareware, or commercial, provided that full credits are given.
  8. You may not sell or distribute this code for profit.
  9. */
  10.  
  11. //#pragma load "MacDump"
  12.  
  13. #ifndef __ICAPI__
  14. #include <ICAPI.h>
  15. #endif
  16.  
  17. #include    "UtilsSys7.h"
  18. #include    "Conversions.h"
  19. #include    "MovableModal.h"
  20. #include    "FabLibResIDs.h"
  21.  
  22. #include    "Disasm.h"
  23. #include    "DoEditDialog.h"
  24. #include    "DoMenu.h"
  25. #include    "DragManSim.h"
  26. #include    "Dump.h"
  27. #include    "Globals.h"
  28. #include    "Preferences.h"
  29. #include    "Main.h"
  30. #include    "Registers.h"
  31.  
  32. #if defined(FabSystem7orlater)
  33.  
  34. static Boolean PrefsPreProcessKey(EventRecord *thEv, DialogPtr theD);
  35. static pascal Boolean PrefsFilter(DialogPtr theD, EventRecord *thEv, short *iHit);
  36. static void SetTopLeft(Point *thePt, WindowPtr w);
  37.  
  38. #pragma segment Rare
  39.  
  40. enum {
  41. kItemRememberWindPos = 3,
  42. kItemContinuousDumpScroll,
  43. kItemInfLoopsDetect,
  44. kItemInitialPCVal,
  45. kItemInitialSPVal,
  46. kItemStackSize
  47. };
  48.  
  49. void Preferences(void)
  50. {
  51. enum {
  52. kDLOG_Prefs = 262
  53. };
  54.  
  55. Str255    InitPCStr, InitSPStr, StackSizeStr;
  56. long    dummy;
  57.  
  58. dialogItems    things[] = {{ ok, 0, 1L },
  59.                         { cancel, 0, 0L },
  60.                         { kItemRememberWindPos, 0, 0L },
  61.                         { kItemContinuousDumpScroll, 0, 0L },
  62.                         { kItemInfLoopsDetect, 0, 0L },
  63.                         { kItemInitialPCVal, 0, 0L },
  64.                         { kItemInitialSPVal, 0, 0L },
  65.                         { kItemStackSize, 0, 0L },
  66.                         { 0, 0, 0L}
  67.                         };
  68.  
  69. things[kItemRememberWindPos-1].refCon = gPrefs.remembWind;
  70. things[kItemContinuousDumpScroll-1].refCon = gPrefs.NeXTScroll;
  71. things[kItemInfLoopsDetect-1].refCon = gPrefs.infLoopsDetect;
  72. things[kItemInitialPCVal-1].refCon = (long)&InitPCStr;
  73. things[kItemInitialSPVal-1].refCon = (long)&InitSPStr;
  74. things[kItemStackSize-1].refCon = (long)&StackSizeStr;
  75.  
  76. ShortToHexString(gPrefs.DefPCValue, InitPCStr);
  77. ShortToHexString(gPrefs.DefSPValue, InitSPStr);
  78. MyNumToString(gPrefs.DefStkSize, StackSizeStr);
  79.  
  80. if (HandleMovableModalDialog(things, gPrefs.remembWind ? &gPrefs.generalPrefsTL : nil, nil, nil, nil, nil, nil,
  81.     AdjustMenus,
  82.     Handle_My_Menu,
  83.     DomyKeyEvent,
  84.     PrefsPreProcessKey,
  85.     nil,
  86.     DoUpdate,
  87.     DoActivate,
  88.     DoHighLevelEvent,
  89.     DoOSEvent,
  90.     DoIdle,
  91.     ULONG_MAX,
  92.     kDLOG_Prefs) == ok) {
  93.     gPrefs.remembWind = things[kItemRememberWindPos-1].refCon;
  94.     gPrefs.NeXTScroll = things[kItemContinuousDumpScroll-1].refCon;
  95.     gPrefs.infLoopsDetect = things[kItemInfLoopsDetect-1].refCon;
  96.     HexStringToShort(InitPCStr, (short *)&gPrefs.DefPCValue);
  97.     HexStringToShort(InitSPStr, (short *)&gPrefs.DefSPValue);
  98.     StringToNum(StackSizeStr, &dummy);
  99.     gPrefs.DefStkSize = dummy;
  100.     }
  101. }
  102.  
  103. Boolean PrefsPreProcessKey(EventRecord *thEv, DialogPtr theD)
  104. {
  105. short    iHit;
  106. unsigned char    keypressed;
  107. Boolean    result = true;
  108.  
  109. keypressed = CHARFROMMESSAGE(thEv->message);
  110. if ((keypressed >= 'a') && (keypressed <= 'z')) {
  111.     keypressed -= 'a' - 'A';    /* cambiare! */
  112.     CHARFROMMESSAGE(thEv->message) = keypressed;
  113.     }
  114. iHit = ((DialogPeek)theD)->editField + 1;
  115. if (keypressed >= 32 && ((thEv->modifiers & cmdKey) == 0)) {
  116.     switch (iHit) {
  117.         case kItemInitialPCVal:
  118.         case kItemInitialSPVal:
  119.             result = ( Munger((Handle)GetString(kSTR_HEXALLOWED), 1L, &keypressed,
  120.                             1L, 0L, 0L) >= 0L );
  121.             break;
  122.         case kItemStackSize:
  123.             result = ( Munger((Handle)GetString(kSTR_DECALLOWED), 1L, &keypressed,
  124.                             1L, 0L, 0L) >= 0L );
  125.             break;
  126.         }
  127.     }
  128. return result;
  129. }
  130.  
  131. /* PrefsFilter: the filterProc routine for the Prefs dialog */
  132. /*
  133. static pascal Boolean PrefsFilter(DialogPtr theD, EventRecord *thEv, short *iHit)
  134. {
  135. GrafPtr    savePort;
  136. unsigned char    keypressed;
  137. register Boolean    retVal;
  138.  
  139. switch(thEv->what) {
  140.     case keyDown    :
  141.     case autoKey    :
  142.         keypressed = CHARFROMMESSAGE(thEv->message);
  143.         if ((keypressed >= 'a') && (keypressed <= 'z')) {
  144.             keypressed -= 'a' - 'A';    // cambiare!
  145.             CHARFROMMESSAGE(thEv->message) = keypressed;
  146.             }
  147.         if ((keypressed >= 32) && (keypressed != 0x7F) && ((thEv->modifiers & cmdKey) == 0)) {
  148.             *iHit = ((DialogPeek)theD)->editField + 1;
  149.             switch( *iHit ) {
  150.                 case kItemInitialPCVal:
  151.                 case kItemInitialSPVal:
  152.                     return( Munger((Handle)GetString(kSTR_HEXALLOWED), 1L, &keypressed,
  153.                                     1L, 0L, 0L) < 0L );
  154.                     break;
  155.                 case kItemStackSize:
  156.                     return( Munger((Handle)GetString(kSTR_DECALLOWED), 1L, &keypressed,
  157.                                     1L, 0L, 0L) < 0L );
  158.                     break;
  159.                 }
  160.             }
  161.         break;
  162.     case updateEvt:
  163.         if (theD != (DialogPtr)thEv->message) {
  164.             DoUpdate(thEv);
  165.             *iHit = kfakeUpdateItem;
  166.             return true;
  167.             }
  168.         break;
  169.     case activateEvt:
  170.         if (theD != (DialogPtr)thEv->message) {
  171.             DoActivate(thEv);
  172.             *iHit = kfakeUpdateItem;
  173.             return true;
  174.             }
  175.         break;
  176.     }
  177. GetPort(&savePort);
  178. SetPort(theD);
  179. retVal = StdFilterProc(theD, thEv, iHit);
  180. SetPort(savePort);
  181. return retVal;
  182. }
  183. */
  184. #pragma segment CleanUp
  185.  
  186. /* SavePreferencesFile: in the end we save our gPrefs */
  187.  
  188. void SavePreferencesFile(void)
  189. {
  190. ParamBlockRec    myPB;
  191. EventRecord    dummyEv;
  192. FSSpec    myFSS;
  193. register Handle    myStrHand;
  194. short    prefsFRefNum;
  195. Boolean    targetFolder, isAnAlias;
  196. register OSErr    err;
  197. register SignedByte    oldState;
  198.  
  199. oldState = WantThisHandleSafe(myStrHand = (Handle)GetString(kSTR_PREFSFILENAME));
  200. if ((err = FindFolder(kOnSystemDisk, kPreferencesFolderType, kCreateFolder,
  201.             &myFSS.vRefNum, &myFSS.parID)) == noErr) {
  202.     err = FSMakeFSSpecCompat(myFSS.vRefNum, myFSS.parID, (ConstStr255Param)*myStrHand, &myFSS);
  203.     if ((err == noErr) || (err == fnfErr)) {
  204.         if (err == fnfErr)
  205. #ifndef __SCRIPT__
  206. #define    smSystemScript    -1
  207. #endif
  208.             err = FSpCreateCompat(&myFSS, '????', kPreferencesFolderType, smSystemScript);
  209.         if (err == noErr)
  210.             if ((err = ResolveAliasFile(&myFSS, true, &targetFolder, &isAnAlias)) == noErr)
  211.                 if (targetFolder)
  212.                     err = paramErr;
  213.                 else
  214.                     if ((err = FSpOpenDFCompat(&myFSS, fsWrPerm, &prefsFRefNum)) == noErr) {
  215.                         SetTopLeft(&gPrefs.AnimTopLeft, gWPtr_Animation);
  216.                         SetTopLeft(&gPrefs.RegsTopLeft, gWPtr_Registers);
  217.                         gPrefs.RegsBase = GetControlValue(Ctrl_Base);
  218.                         gPrefs.IOUserState = (*(WStateDataHandle)((WindowPeek)gWPtr_IO)->dataHandle)->userState;
  219.                         SetTopLeft(&gPrefs.MProgTopLeft, gWPtr_Microprogram_Ed);
  220.                         SetTopLeft(&gPrefs.DumpTopLeft, gWPtr_Dump);
  221.                         gPrefs.DumpHeight = PRCT_B(gWPtr_Dump) - PRCT_T(gWPtr_Dump);
  222.                         gPrefs.DumpScrollVal = GetControlValue(dumpVScroll);
  223.                         SetTopLeft(&gPrefs.DisasmTopLeft, gWPtr_Disasm);
  224.                         gPrefs.DisasmHeight = PRCT_B(gWPtr_Disasm) - PRCT_T(gWPtr_Disasm);
  225.                         gPrefs.DisasmScrollVal = GetControlValue(disasmVScroll);
  226.                         gPrefs.AnimVisible = ((WindowPeek)gWPtr_Animation)->visible;
  227.                         gPrefs.RegsVisible = ((WindowPeek)gWPtr_Registers)->visible;
  228.                         gPrefs.IOVisible = ((WindowPeek)gWPtr_IO)->visible;
  229.                         gPrefs.DumpVisible = ((WindowPeek)gWPtr_Dump)->visible;
  230.                         gPrefs.DisasmVisible = ((WindowPeek)gWPtr_Disasm)->visible;
  231.                         myPB.ioParam.ioCompletion = 0L;
  232.                         myPB.ioParam.ioRefNum = prefsFRefNum;
  233.                         myPB.ioParam.ioBuffer = (Ptr)&gPrefs;
  234.                         myPB.ioParam.ioReqCount = sizeof(struct myprefs);
  235.                         myPB.ioParam.ioPosMode = fsFromStart | kNoCacheMask;
  236.                         myPB.ioParam.ioPosOffset = 0L;
  237.                         (void)PBWriteAsync(&myPB);
  238.                         while (myPB.ioParam.ioResult > 0) {
  239.                             SystemTask();
  240.                             (void)EventAvail(everyEvent, &dummyEv);
  241.                             }
  242.                         (void)FSClose(prefsFRefNum);
  243.                         if ((err = myPB.ioParam.ioResult) == noErr)
  244.                             err = AddSTRRes2Doc(&myFSS, '????', kPreferencesFolderType,
  245.                                                 kSTR_NOOPENORPRINT, smSystemScript);
  246.                         }
  247.         }
  248.     }
  249. HSetState(myStrHand, oldState);
  250. }
  251.  
  252. /* SetTopLeft: common routine which calculates the topLeft coordinate
  253. of a GrafPort’s portRect in global coordinates;
  254. it _changes_ the current port */
  255.  
  256. static void SetTopLeft(Point *thePt, GrafPtr w)
  257. {
  258. *thePt = topLeft(w->portRect);
  259. SetPort(w);
  260. LocalToGlobal(thePt);
  261. }
  262.  
  263. void CleanUp(void)
  264. {
  265. if (gDragManagerActive) {
  266.     MyRemoveHWindow(gWPtr_Registers);
  267.     MyRemoveHWindow(gWPtr_Disasm);
  268.     MyRemoveHWindow(gWPtr_Dump);
  269.     }
  270. if (gICinst)
  271.     (void)ICStop(gICinst);
  272. }
  273.  
  274. #endif
  275.  
  276.